//
//  EatViewController.m
//  项目A --半糖
//
//  Created by romana on 16/5/16.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "EatViewController.h"
#import "AFNetworking.h"
#import "EatVCModel.h"
#import "EatSecondModel.h"
#import "EatTableViewCell.h"

static CGFloat const CELLH = 280;


#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height


@interface EatViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *myArrayTwo;

@end

@implementation EatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.myArray = [NSMutableArray array];
    [self getData];
    [self read];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - 64 - 44 - 49) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[EatTableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    
    
    
    
    // Do any additional setup after loading the view.
}

-(void)read{
    self.myArrayTwo = [NSMutableArray array];
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *eventArrayPath = [path stringByAppendingPathComponent:@"EateventArray.txt"];
    NSDictionary *dataDic = [NSDictionary dictionaryWithContentsOfFile:eventArrayPath];

    EatVCModel *eatModel = [[EatVCModel alloc] init];
    [eatModel setValuesForKeysWithDictionary:dataDic];
    for (EatSecondModel *model in eatModel.topic) {
        [self.myArrayTwo addObject:model];
    }


}

-(void)getData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://open3.bantangapp.com/topic/list?app_id=com.jzyd.BanTang&app_installtime=1434705307&app_versions=5.7&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&os_versions=9.3.1&page=0&pagesize=20&scene=15&screensize=1242&track_device_info=iPhone7%2C1&track_deviceid=C2D3B460-4E0E-4639-A030-D274CFC43D50&v=12%20HTTP/1.1" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *bigDic = responseObject;
        NSMutableDictionary *dataDic = [bigDic objectForKey:@"data"];
        //缓存
        NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        NSString *eventArrayPath = [path stringByAppendingPathComponent:@"EateventArray.txt"];
        //(2)将字典写入文件
        [dataDic writeToFile:eventArrayPath atomically:YES];
        
        EatVCModel *eatModel = [[EatVCModel alloc] init];
        [eatModel setValuesForKeysWithDictionary:dataDic];
        for (EatSecondModel *model in eatModel.topic) {
            [self.myArray addObject:model];
        }
        
        [self.myTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];




}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return CELLH;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.myArray.count == 0) {
        return  self.myArrayTwo.count;
    } else {
        return self.myArray.count;
    }}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (self.myArray.count == 0) {
        cell.EatSModel = [self.myArrayTwo objectAtIndex:indexPath.row];

    } else {
        cell.EatSModel = [self.myArray objectAtIndex:indexPath.row];

    }
 //   cell.textLabel.text = @"666";
    return cell;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
